package com.eshop.controller;

import com.eshop.model.Goods;
import com.eshop.model.myPojo.CartGoods;
import com.eshop.service.GoodsDetailService;
import com.eshop.service.Impl.GoodsDetailServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/cart")
public class CartController extends HttpServlet {
    GoodsDetailService goodsDetailService = new GoodsDetailServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        String action = req.getParameter("action");

        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object _LOGIN_USER_ = req.getSession().getAttribute("_LOGIN_USER_");
        if (_LOGIN_USER_ == null){
            resp.getWriter().write("{\"success\":false}");
            return;
        }

        try {
            int goodsId = Integer.parseInt(req.getParameter("goodsId"));
            int goodsNum = Integer.parseInt(req.getParameter("goodsNum"));

            Map<Integer,CartGoods> cartGoodsList = (Map<Integer, CartGoods>) req.getSession().getAttribute("cartGoodsList");

            if (cartGoodsList == null || cartGoodsList.size() == 0 || cartGoodsList.get(goodsId) == null){
                Goods goods = goodsDetailService.getGoodsById(goodsId);
                CartGoods cartGoods = new CartGoods(goods.getGoods_id(),goods.getGoods_name(),goods.getGoods_discount(),goods.getGoods_pic(),goodsNum);
                if (cartGoodsList == null) cartGoodsList = new HashMap<>();
                cartGoodsList.put(cartGoods.getGoods_id(),cartGoods);
            }
            else {
                CartGoods cartGoodsSave = cartGoodsList.get(goodsId);
                cartGoodsSave.setGoods_num(cartGoodsSave.getGoods_num()+goodsNum);
            }

            req.getSession().setAttribute("cartGoodsList",cartGoodsList);
            resp.getWriter().write("{\"success\":true}");
        }catch (Exception e) {
            resp.getWriter().write("{\"success\":false}");
            e.printStackTrace();
        }
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer cartGoodsId = Integer.parseInt(req.getParameter("cartGoodsId"));
        Map<Integer,CartGoods> cartGoodsList = (Map) req.getSession().getAttribute("cartGoodsList");
        cartGoodsList.remove(cartGoodsId);
        if (cartGoodsList.size() != 0) {
            req.getSession().setAttribute("cartGoodsList",cartGoodsList);
        }
        else {
            req.getSession().removeAttribute("cartGoodsList");
        }
    }
}
